package com.seecen.controller;

import com.seecen.common.BaseResponse;
import com.seecen.pojo.entity.Category;
import com.seecen.pojo.entity.Coupon;
import com.seecen.pojo.dto.EvaluationAddRequest;
import com.seecen.pojo.entity.Evaluation;
import com.seecen.pojo.entity.ServiceEntity;
import com.seecen.service.CategoryService;
import com.seecen.service.CouponService;
import com.seecen.service.EvaluationService;
import com.seecen.service.ServiceService;
import com.seecen.utils.JwtHelper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 小辛同学
 * @CreateTime: 2025-10-17
 * @Description: 首页相关接口
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {

    @Resource
    private ServiceService serviceService;

    @Resource
    private CouponService couponService;

    @Resource
    private JwtHelper jwtHelper;
    @Resource
    private CategoryService categoryService;
    @Resource
    private EvaluationService evaluationService;

    /**
     * 获取服务列表
     */
    @GetMapping("/services")
    public BaseResponse<List<ServiceEntity>> getServices() {
        return serviceService.getServices();
    }

    /**
     * 获取可用优惠券列表
     */
    @GetMapping("/coupons")
    public BaseResponse<List<Coupon>> getAvailableCoupons() {
        return couponService.getAvailableCoupons();
    }

    /**
     * 领取优惠券
     */
    @PostMapping("/coupon/claim")
    public BaseResponse<String> claimCoupon(@RequestParam Integer couponId, HttpServletRequest request) {
        // 从session或token中获取用户ID，这里简化处理，假设用户ID为1
        // 实际项目中应该从登录状态中获取
        Integer userId = getUserIdFromRequest(request);
        if (userId == null) {
            return couponService.claimCoupon(1, couponId); // 临时使用用户ID为1
        }
        return couponService.claimCoupon(userId, couponId);
    }

    /**
     * 一键领取所有优惠券
     */
    @PostMapping("/coupon/claim-all")
    public BaseResponse<String> claimAllCoupons(HttpServletRequest request) {
        // 从session或token中获取用户ID，这里简化处理，假设用户ID为1
        Integer userId = getUserIdFromRequest(request);
        if (userId == null) {
            return couponService.claimAllCoupons(1); // 临时使用用户ID为1
        }
        return couponService.claimAllCoupons(userId);
    }

    /**
     * 从请求中获取用户ID
     * 通过JWT token解析获取用户ID
     */
    private Integer getUserIdFromRequest(HttpServletRequest request) {
        try {
            // 从请求头中获取token
            String token = request.getHeader("token");
            if (token == null || token.trim().isEmpty()) {
                log.warn("请求头中未找到token");
                return null;
            }
            
            // 检查token是否过期
            if (jwtHelper.isExpiration(token)) {
                log.warn("token已过期");
                return null;
            }
            
            // 从token中解析用户ID
            Long userId = jwtHelper.getUserId(token);
            if (userId == null) {
                log.warn("无法从token中解析用户ID");
                return null;
            }
            
            // 将Long类型转换为Integer类型
            return userId.intValue();
        } catch (Exception e) {
            log.error("从请求中获取用户ID失败", e);
            return null;
        }
    }
    @GetMapping("/admin/categoryList")
    public BaseResponse<List<Category>> getIndexCategory(HttpServletRequest request) {
        return categoryService.getIndexCategory();
    }


    //发布评价
    @PostMapping("/addEvaluation")
    public BaseResponse<?> addIndexEvaluation(@RequestBody EvaluationAddRequest request) {
        return evaluationService.addIndexEvaluation(request);
    }

    //获取评价
    @GetMapping("/getIndexEvaluation")
    public BaseResponse<?> getIndexEvaluation(HttpServletRequest request) {
        return evaluationService.getIndexEvaluation();
    }


}
